/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     struct TreeNode *left;
 *     struct TreeNode *right;
 * };
 */
 /**
  * Note: The returned array must be malloced, assume caller calls free().
  */
int _cnt(struct TreeNode* root)
{
    if (root == NULL)return 0;
    return 1 + _cnt(root->left) + _cnt(root->right);
}
void _preorderTraversal(struct TreeNode* root, int* ans, int* cnt)
{
    if (root == NULL)return;
    printf("%d ", root->val);
    ans[(*cnt)++] = root->val;
    _preorderTraversal(root->left, ans, cnt);
    _preorderTraversal(root->right, ans, cnt);
}
int* preorderTraversal(struct TreeNode* root, int* returnSize) {
    int sz = _cnt(root);
    //printf("%d\n",sz);
    int* ans = (int*)malloc(sizeof(int) * sz);
    int res = 0;
    _preorderTraversal(root, ans, &res);
    *returnSize = res;
    // printf("%d\n",res);
    return ans;
}